import axios from 'axios'
import { baseURL }   from '@/config'
import { Loading } from 'element-ui'

let loadinginstance, loadCount = 0,
    loadingArray = [];
class HttpRequest {
   constructor(basrUrl = baseURL) {
      this.basrUrl = basrUrl
      //this.queue = {}
   }
   getInsideConfing() {
      const config = {
         baseUrl: this.baseUrl,
         headers: {}
      }
     
      return config
   }
   //axios设置请求拦截器
   interceptors(instance) {
      instance.interceptors.request.use(config => {
        loadCount++
        loadinginstance = Loading.service({
          text: '请稍等', //内容
          background: 'rgba(28, 36, 51, 0.5) ', //样式
          target: document.querySelector('#Zbcp >.table') //绑定DOM
        })
        loadingArray.push(loadinginstance)
          
        return config
         },
         err => {
          loadCount --
          if (!loadCount) {
            loadingArray.forEach(item => item.close())
          }
            return Promise.reject(err);
         })
      //axios添加响应拦截器
      instance.interceptors.response.use(res => {
        loadCount --
        if (!loadCount) {
          loadingArray.forEach(item => item.close())
        }
        let { data } = res
         return data
      }, err => {
         return Promise.reject(err)
      })
   }



   request(options) {
      const instance = axios.create()
      options = Object.assign(this.getInsideConfing(), options)
      this.interceptors(instance)
      return instance(options)
   }

}

export default HttpRequest